//
//  IMViewController.m
//  Gunz
//
//  Created by Joggy on 16/8/24.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "IMViewController.h"
#import "QYNotice.h"

@interface IMViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    
    
    
}

@end

@implementation IMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"图片拍摄";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tapButton.frame = CGRectMake(0, 0, 120, 66);
    tapButton.center = CGPointMake(screenWidth/2, screenHeight*2/3);
    tapButton.layer.cornerRadius = 6;
    [tapButton setTitle:@"点击拍摄" forState:UIControlStateNormal];
    [tapButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    tapButton.backgroundColor = CUSTOM_GREEN;
    [tapButton addTarget:self action:@selector(captureImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tapButton];
}

- (void)captureImage {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    picker.allowsEditing = false;
    picker.delegate = self;
    [self presentViewController:picker animated:true completion:nil];
}

- (void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    
    [picker dismissViewControllerAnimated:true completion:^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入文件名" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"图片名";
        }];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UITextField *textField = alert.textFields[0];
            NSString *superPath = [NSString stringWithFormat:@"%@/%@", documentBasePath, GZUserImagePath];
            NSFileManager *manager = [NSFileManager defaultManager];
            if(![manager fileExistsAtPath:superPath]) {
                [manager createDirectoryAtPath:superPath withIntermediateDirectories:true attributes:nil error:nil];
            }
            
            if([textField.text isEqualToString:@""]) {
                textField.text = [GZConst getCurrentDateFormate];
            }
            
            NSString *savePath = [NSString stringWithFormat:@"%@/%@/%@.jpg", documentBasePath, GZUserImagePath, textField.text];
            if([imageData writeToFile:savePath atomically:true]) {
                
            }
            else {
                [QYNotice showError:@"保存失败" toView:self.view];
                [QYNotice dismissWithDelay:3];
            }
            
        }]];
        [self presentViewController:alert animated:true completion:nil];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
